﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Data;
using TBM.Filters;
using TBM.Attributes;

namespace TBM.Controllers
{
    public partial class UserController : MasterController
    {
        [Description("个人资料")]
        [TBMAuthorize("用户管理", "个人资料")]
        public ActionResult UserProfile()
        {
            ViewBag.UserInfo = CurrUser;

            if (ViewBag.UserInfo == null)
            {
                return RedirectToAction("Code500", "Error");
            }

            return View("Profile");
        }

        [ParentAction("UserProfile")]
        [Description("用户信息概览")]
        [TBMAuthorize("用户管理", "用户信息概览")]
        public ActionResult ProfileOverview()
        {
            cmd.CommandText = "p_GetSysLog";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@UID", SqlDbType.VarChar, 15);
            cmd.Parameters["@UID"].Value = CurrUser.UID;
            List<Models.Feedback> sysLog = new List<Models.Feedback>();

            using (SqlDataReader dr = cmd.ExecuteReader())
            {
                while (dr.Read())
                {
                    sysLog.Add(new Models.Feedback
                    {
                        ID = (int)dr["ID"],
                        Type = Convert.ToInt16(dr["Type"]),
                        Message = dr["Message"].ToString(),
                        CreateDate = Convert.ToDateTime(dr["CreateDate"])
                    });
                }
            }

            ViewBag.SysLog = sysLog; 

            return View();
        }


        [ParentAction("UserProfile")]
        [Description("个人账户设置")]
        [TBMAuthorize("用户管理", "个人账户设置")]
        public ActionResult ProfileSetting()
        {
            ViewBag.UserInfo = CurrUser;

            if (ViewBag.UserInfo == null)
            {
                return RedirectToAction("C500", "Error");
            }

            cmd.CommandText = "p_ModulesInUser";
            cmd.CommandType = CommandType.StoredProcedure; 
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@UID", SqlDbType.VarChar, 15);
            cmd.Parameters["@UID"].Value = CurrUser.UID;
            List<Models.Module> Modules = new List<Models.Module>();
            Dictionary<Models.Module, List<Models.Module>> ModuleList = new Dictionary<Models.Module, List<Models.Module>>();

            using (SqlDataReader dr = cmd.ExecuteReader())
            {
                while (dr.Read())
                {
                    Modules.Add(new Models.Module
                    {
                        ID = (int)dr["ID"],
                        PID = (int)dr["PID"],
                        ControllerName = dr["ControllerName"].ToString(),
                        ControllerDescr = dr["ControllerDescr"].ToString(),
                        ActionName = dr["ActionName"].ToString(),
                        ActionDescr = dr["ActionDescr"].ToString(),
                        IsActived = (bool)dr["IsActived"],
                        IsChecked = (bool)dr["IsChecked"]
                    });
                }
            }

            foreach (Models.Module module in Modules.Where(item => item.PID == 0))
            {
                ModuleList.Add(module, Modules.Where(item => item.PID == module.ID).ToList());
            }

            ViewBag.ModuleList = ModuleList;

            return View();
        }


        [ParentAction("UserProfile")]
        [Description("个人头像设置")]
        [TBMAuthorize("用户管理", "个人头像设置")]
        public JsonResult UpdateProfileAvatar()
        {
            return _UpdateAvatar(CurrUser.UID);
        }


        [ParentAction("UserProfile")]
        [Description("更新个人信息")]
        [TBMAuthorize("用户管理", "更新个人信息")]
        public JsonResult UpdateProfileInfo(string firstname, string lastname, string email)
        {
            return _UpdateUser(CurrUser.UID, firstname, lastname, email, false);
        }


        [ParentAction("UserProfile")]
        [Description("个人密码设置")]
        [TBMAuthorize("用户管理", "个人密码设置")]
        public JsonResult UpdateProfilePWD(string newpassword, string currpassword)
        {
            return _UpdatePWD(CurrUser.UID, newpassword, currpassword);
        }
    }
}
